/**
 * 
 */
package bpel.variables;

import java.net.MalformedURLException;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import bpel.types.BPELType;

/**
 * @author Maurice & Topia
 *
 */
public class BPELStandardVariable extends BPELVariable {

	private BPELType type;
	private String name;
	private String value;

	public BPELStandardVariable(BPELType type, String name) {
		super();
		this.type = type;
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Override
	public String toString() {
		return type.getName() + " : " + name;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String query(String query) {
		return value;
	}

	@Override
	public void set(String query, String value) {
		this.value = value;
	}

	@Override
	public void parse(String XMLDescription) {
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new URL(XMLDescription));
			
			Node node = document.selectSingleNode( "//" + type.getTypeNodeName() + "[@name='" + name + "']" );
			
			setValue(node.getStringValue());
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Document getXMLDescription() {
		System.out.println("TODO implement BPELComplexVariable getXMLDescriprion method");
		System.out.println("Variable " + name + " value = " + value);
		
		return null;
	}
}
